Storage.prototype.setExpire = (key, value, expire) => {
    let obj = {
        data: value,
        time: Date.now(),
        expire: expire
    };
    //localStorage 设置的值不能为对象,转为json字符串
    localStorage.setItem(key, JSON.stringify(obj));
}

Storage.prototype.getExpire = key => {
    let val = localStorage.getItem(key);
    if (!val) {
        return val;
    }

    val = JSON.parse(val);
    console.log(Date.now() - val.time)
    console.log(val.expire)
    if (Date.now() - val.time > val.expire) {
        localStorage.removeItem(key);
        return null;
    }
    return val.data;
}
/*工具类*/
var CoreUtil = (function () {
    var coreUtil = {};

    var baseurl = 'http://127.0.0.1:9527/api'; //配置网关后台地址

    /*GET*/
    coreUtil.sendGet = function(url, params, ft){
        this.sendAJAX(url, params, ft, "GET")
    }

    /*POST*/
    coreUtil.sendPost = function(url, params, ft){
        this.sendAJAX(url, JSON.stringify(params), ft, "POST")
    }
    /*PUT*/
    coreUtil.sendPut = function(url, params, ft){
        this.sendAJAX(url, JSON.stringify(params), ft, "PUT")
    }
    /*DELETE*/
    coreUtil.sendDelete = function(url, params, ft){
        this.sendAJAX(url, JSON.stringify(params), ft, "DELETE")
    }

    /*ajax*/
    coreUtil.sendAJAX = function(url, params, ft, method){
        $.support.cors = true;
        $.ajax({
            url: baseurl + url,
            cache: false,
            async: true,
            data: params,
            type: method,
            dataType: "json",
            headers: {
                "Authorization" : localStorage.getExpire("accessToken")
            },
            contentType:"application/json",
            success: function (res) {
                if (res.code==200){
                    if(ft!=null&&ft!=undefined){
                        ft(res);
                    }
                } else {
                    mlayer.alert({
                        title: '消息',
                        content: res.msg,
                        btn: 'OK'
                    });
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                mlayer.alert({
                    title: '消息',
                    content: textStatus,
                    btn: 'OK'
                });
            }
        })
    }

    //判断字符是否为空的方法
    coreUtil.isEmpty = function(obj,message){
        if(typeof obj == "undefined" || obj == null || obj == ""){
            mlayer.alert({
                title: '消息',
                content: message,
                btn: 'OK'
            });
            return true;
        }else{
            return false;
        }
    }

    coreUtil.getQueryVariable = function(obj){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == obj){return pair[1];}
        }
        return(false);
    }

    //字典数据回显
    coreUtil.selectDictLabel = function (datas, value) {
        datas = JSON.parse(datas);
        var label = "";
        $.each(datas, function(index, dict) {
            if (dict.value == ('' + value)) {
                label = dict.label;
                return false;
            }
        });
        //匹配不到，返回未知
        if (CoreUtil.isEmpty(label)) {
            return "未知";
        }
        return label;
    }


    return coreUtil;
})(CoreUtil, window);
